package com.aridia.jumpgun;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;

public class GamePlayActivity extends BaseGameActivity 
{
	 private static final int CAMERA_WIDTH = 720;
     private static final int CAMERA_HEIGHT = 480;
     
  // ===========================================================
  // Fields
  // ===========================================================
     private Camera mCamera;
     private BitmapTextureAtlas mBitmapTextureAtlas;
     private TextureRegion mFaceTextureRegion;
     private TiledTextureRegion mTilesetTexture;
     private TMXTiledMap mTMXTiledMap;
     

	public Engine onLoadEngine() 
	{
		 this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
         return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	public void onLoadResources() 
	{
		  this.mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
		  
		  //path to textures
          BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
          
          //load textureRegion
          this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 64, 0);
          this.mTilesetTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "tileset2.png", 0, 0, 2, 1); // 72x128

          //load texture
          this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
		
	}

	public Scene onLoadScene() 
	{
		//FPS Logger
		  this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0, 0, 0));
        
        //load map 
        try 
        {
            final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, null);
            this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "level/map1.tmx");
        } 
        catch (final TMXLoadException tmxle) 
        {
            Debug.e(tmxle);
        }

        final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
        
        scene.attachChild(tmxLayer);
        
        return scene;
	}

	public void onLoadComplete() 
	{
		// TODO Auto-generated method stub		
	}

}
